package top.zhongyy.controller.admin;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sun.org.apache.regexp.internal.REUtil;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import top.zhongyy.pojo.Blog;
import top.zhongyy.pojo.User;
import top.zhongyy.service.BlogService;
import top.zhongyy.service.TagService;
import top.zhongyy.service.TypeService;
import top.zhongyy.vo.BlogQuery;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * <p>
 * 后台博客管理列表
 * </p>
 *
 * @author Andy
 * @since 2021-03-25
 */
@Controller
@RequestMapping("/admin")
@SuppressWarnings("all")
public class BlogController {
    
    @Autowired
    private BlogService blogService;
    
    @Autowired
    private TypeService typeService;
    
    @Autowired
    private TagService tagService;
    
    
    /**
     * @param model
     * @param pageNum
     * @author Andy
     * @date: 2021/3/26 10:02
     */
    @ApiOperation(value = "获取博客分页列表")
    @GetMapping("/blogs")
    public String blogList(Model model, @RequestParam(defaultValue = "1", value = "pageNum") Integer pageNum) {
        model.addAttribute("types", typeService.list());
        PageHelper.startPage(pageNum, 5, "update_time DESC");
        List<Blog> blogs = blogService.getAdminBlogList();
        PageInfo<Blog> pageInfo = new PageInfo<>(blogs);
        model.addAttribute("page", pageInfo);
        return "admin/blogs";
    }
    
    
    /**
     * @param
     * @Desc: 因为新增编辑共用一个界面，所以需要传递Blog对象，不然会报NPE
     * @author Andy
     * @date: 2021/3/26 10:11
     */
    @ApiOperation(value = "跳转到新增博客页面")
    @GetMapping("/blogs/input")
    public String toAdd(Model model) {
        model.addAttribute("types", typeService.list());
        model.addAttribute("tags", tagService.list());
        model.addAttribute("blog", new Blog());
        return "admin/blogs_release";
    }
    
    @ApiOperation(value = "条件查询帖子")
    @PostMapping("/blogs/search")
    public String searchBlog(@RequestParam(defaultValue = "1", value = "pageNum") Integer pageNum,
                             BlogQuery blogQuery, Model model) {
        PageHelper.startPage(pageNum, 5, "update_time desc");
        List<Blog> blogs = blogService.searchBlog(blogQuery);
        PageInfo<Blog> pageInfo = new PageInfo<Blog>(blogs);
        for (Blog blog : pageInfo.getList()) {
            blog.setType(typeService.getById(blog.getTypeId()));
        }
        model.addAttribute("page", pageInfo);
        return "admin/blogs :: blogList";
    }
    
    
    /**
     * @param blog
     * @param session
     * @param attributes
     * @author Andy
     * @date: 2021/3/26 10:50
     */
    @ApiOperation(value = "新增博客操作")
    @PostMapping("/blogs")
    public String addBlog(Blog blog, HttpSession session, RedirectAttributes attributes) {
        blog.setUserId(((User) session.getAttribute("user")).getId());
        int flag;
        if (blog.getId() == null) {
            flag = blogService.saveBlog(blog);
        } else {
            flag = blogService.updateBlog(blog);
        }
        if (flag == 0) {
            attributes.addFlashAttribute("message", "操作失败");
        } else {
            attributes.addFlashAttribute("message", "操作成功");
        }
        return "redirect:/admin/blogs";
    }
    
    
    /**
     * @param model
     * @param id
     * @author Andy
     * @date: 2021/3/26 10:50
     */
    @ApiModelProperty(value = "跳转到修改界面")
    @GetMapping("/blogs/{id}/input")
    public String toEdit(Model model, @PathVariable("id") Long id) {
        model.addAttribute("tags", tagService.list());
        model.addAttribute("types", typeService.list());
//        Blog blog = blogService.getById(id);
//        blog.setTags(tagService.parseTagIds(blog.getTagIds()));
        model.addAttribute("blog", blogService.getById(id));
        return "admin/blogs_release";
    }
    
    
    /**
     * @param id
     * @param attributes
     * @author Andy
     * @date: 2021/3/26 10:50
     */
    @ApiOperation(value = "根据 id 删除")
    @GetMapping("/blogs/{id}/delete")
    public String deleteBlogById(@PathVariable("id") Long id, RedirectAttributes attributes) {
        int deleteBlog = blogService.deleteBlog(id);
        if (deleteBlog == 0) {
            attributes.addFlashAttribute("message", "删除失败");
        } else {
            attributes.addFlashAttribute("message", "删除成功");
        }
        return "redirect:/admin/blogs";
    }
    
    
}

